'use strict';

const Controller = require('egg').Controller;

// 时间戳转化
const timestampToTime = timestamp => {
    const date = new Date(timestamp);// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
    const Y = date.getFullYear() + '-';
    const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D + h + m + s;
};

const times = timestampToTime(new Date())

// time: timestampToTime(new Date()),
class OrderController extends Controller {
    // 订单管理  获取数据
    async ApiOrder() {
        const { ctx, app } = this;

        const data = await app.mysql.select('orders')

        ctx.body = {
            code: 1,
            data,
        };
    }

    // 删除事件
    async ApiDelOrder() {
        const { ctx, app } = this;

        const { id } = ctx.request.body;

        await app.mysql.delete('orders', { id })

        ctx.body = {
            code: 1,
            msg: '删除成功'
        };
    }

    // 搜索事件
    async ApiSearchOrder() {
        const { ctx, app } = this;

        const { username = '', state = '' } = ctx.request.body;
        console.log(username, state);

        const data = await app.mysql.query(`SELECT * FROM orders WHERE username like '%${username}%' and type like '%${state}%'`)

        ctx.body = {
            code: 1,
            msg: '搜索成功',
            data,
        };
    }

}

module.exports = OrderController;
